/* 
 * File:   Xpinger.cpp
 * Author: minaev
 * 
 * Created on 20 Ноябрь 2013 г., 12:57
 */

#include "Xpinger.h"

Xpinger::Xpinger(XbeeContainer* xcont) {
    this->xcont = xcont;
    this->currentPing = 0;
}

Xpinger::~Xpinger() {
}

void Xpinger::ping() {

    if (this->currentPing >= PING_COUNT) {
        this->procResultPing();
        this->currentPing = 0;
    }

    xcont->pingBroadcast();
    this->currentPing++;

}

void Xpinger::procResultPing() {
    static uint16_t cntXbee;
    static uint8_t cntPings;
    static uint8_t curStat;
    static uint32_t buf;

    cntXbee = *(uint8_t*) xcont->getShareMem()->read(SLN_XCOUNT, 0);

    for (uint16_t i = 0; i < cntXbee; i++) {
        cntPings = *(uint8_t*) xcont->getParam(i, SLN_XPINGS);
        curStat = *(uint8_t*) xcont->getParam(i, SLN_XSTAT);
        Xmsg::printf("Dev %i| Pings: %i Stat: %i\n",i,cntPings,curStat);
        if (cntPings == 0 && curStat != STAT_POWEROFF) {
            xcont->setParam(i, SLN_XREQSTAT, &(buf = STAT_POWEROFF));
            xcont->toNewStatus(i);
        }
        if (cntPings != 0 && curStat == STAT_POWEROFF) {
            xcont->setParam(i, SLN_XREQSTAT, &(buf = STAT_IDLE));
            xcont->toNewStatus(i);
        }
        cntPings = 0;
        xcont->setParam(i, SLN_XPINGS, &cntPings);
    }
    Xmsg::printf("Result ping:\n");
    xcont->getShareMem()->printMem();
}

